package com.u.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.u.character.Role;
import com.u.tool.MessageUtil;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class MessageServlet
 */
@WebServlet("/MessageServlet")
public class MessageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MessageServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		HttpSession session = request.getSession();
		Role role = (Role)session.getAttribute("Role");
		String useracc = role.getUseracc();
		String code = useracc.substring(0,1);
		Boolean m = (code.equals("1") || code.equals("2"));
		String type = request.getParameter("type");
		JSONArray json = new JSONArray();
		int num = 0;
		if(m) {
			if(type.equals("newMessageNum")) {
				num = MessageUtil.getNewMessage();
			}
		}
		json.add(num);
		PrintWriter out = response.getWriter();
		out.println(json.toString());
		out.close();
	}

	
}
